#include "participantMenu.h"
#include "cleanScan.h"
void participantMenu(struct UserVars userData){
	struct ConfigVars *configData;
	loadConfigs("data/Configuracion.txt",&configData);
	struct PlantillaVars *plantillaData;
	loadPlantillas("data/Plantillas.txt",&plantillaData);

	int run=1;
	int op;
	while(run){
        printf("Welcome %s\n",userData.name);
        printf("*****Participant Menu*****\n");
        printf("Select an option\n");
        printf("<1>Create roster\n");
        printf("<2>Configure roster\n");
        printf("<3>List rosters\n");
        printf("<4>Delete roster\n");
        printf("<5>Show ranking\n");
        printf("<6>Exit\n");
        scanf("%d",&op);
		char ch = getchar();
		switch(op){
			case 1:
				createRoster(userData,configData,&plantillaData);
				break;
			case 6:
				run=0;
				break;
		}
	}
	writePlantillas("outputfiles/newPlantillas.txt",plantillaData);
}


void createRoster(struct UserVars userData,struct ConfigVars *configData,struct PlantillaVars **plantillasData){
	char rosterName[30];
	printf("Input roster name\n");
	fgets(rosterName,sizeof(rosterName),stdin);
	cleanString(rosterName);
	printf("\n%s",rosterName);
	printf("Max teams:%d",configData[0].value);
	int numTeams=0;
	int curPlantID=0;
	for(int i=0;i<plantillacount;i++){
		if(!strcmp(userData.userID,plantillasData[i]->userID)){
			numTeams++;
		}
		if(curPlantID==atoi(plantillasData[i]->plantillaID)){
			curPlantID++;
		}
	}
	if(numTeams <= configData[0].value){
		struct PlantillaVars newPlantilla;
		strcpy(newPlantilla.name,rosterName);
		strcpy(newPlantilla.userID,userData.userID);
		newPlantilla.budget=configData[1].value;
		newPlantilla.points=0;
		sprintf(newPlantilla.plantillaID,"00%i",curPlantID);
		addPlantilla(newPlantilla,plantillasData);
	}
}
